<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2020/05/30
 * Time: 09:05
 */

namespace Modules\Admin\Service;

use HiCommon\Repository\AppDownloadRepository;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class AppService
{

    public function get_new_version_list(){
        $data = AppDownloadRepository::app_new_version_list();
        $url = env('API_HOST') . '/share-app';
        $size = 150;
        $arr_data = [];
        if(count($data) >= 1){
            foreach ($data as $key => $item){
                $item['code'] = QrCode::size($size)->generate($url);
                if($item['is_force'] == 1){
                    $item['is_force_name'] = '是';
                }
                else{
                    $item['is_force_name'] = '否';
                }
                switch ($item['type'])
                {
                    case 1:
                        $arr_data['android']['user'] = $item;
                        break;
                    case 2:
                        $arr_data['ios']['user'] = $item;
                        break;
                    case 3:
                        $arr_data['android']['shop'] = $item;
                        break;
                    case 4:
                        $arr_data['ios']['shop'] = $item;
                        break;
                    default:
                        //                    return [];
                }


            }
        }

        return  hello_success('成功',$arr_data);
    }

    /*安卓-用户端-最新版本*/
    public function new_android_user_app(){
        $arr_where = ['type'=>1];
        $result = AppDownloadRepository::base_get_one($arr_where);
        $arr_data = [];
        if(!is_null($result)){
            $arr_data = $result->toarray();
            $arr_data['content'] = explode("\n",$arr_data['content']);

        }

        return  hello_success('成功',$arr_data);
    }
    /*安卓-商户端-最新版本*/
    public function new_android_shop_app(){
        $arr_where = ['type'=>3];
        $result = AppDownloadRepository::base_get_one($arr_where);
        $arr_data = [];
        if(!is_null($result)){
            $arr_data = $result->toarray();
            $arr_data['content'] = explode("\n",$arr_data['content']);

        }

        return  hello_success('成功',$arr_data);
    }

    public function add_one_version($arr_data){
        $result = AppDownloadRepository::base_add_one($arr_data);
        if(empty($result->toarray())){
            return hello_error('添加失败');
        }
        return  hello_success('添加成功');
    }
    /**
     * App下载配置
     * @return array
     */
    public function get_all()
    {
        try {
            $data = AppDownloadRepository::base_get_all()->toArray();
            $url = env('API_HOST') . '/share-app';
            $size = 150;
            $info = [];
            foreach ($data as $key => $item){
                $info[$item['type']] = $item;
                $info[$item['type']]['code'] = QrCode::size($size)->generate($url);
            }

            return hello_success('成功', $info);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 添加标App下载配置
     * @param array $arr_input
     * @return array
     */
    public function store(array $arr_input)
    {
        try {
            foreach ($arr_input as $key => $value) {
                if (empty($value)) {
                    return hello_error('内容不能为空');
                }
                $where = ['id' => $key, 'url' => $value];
                AppDownloadRepository::base_edit_one($where);
            }

            return hello_success('编辑成功');
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }
    /**
     * 获取所有权限
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
/*    public function get_all(){
        try {
            $arr_option = ['order'=>['key'=>'id','value'=>'asc']];
            $data = AppRepository::get_all($arr_option)->toarray();
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }*/

    /**
     * 获取分页数据
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list()
    {
        try {

            $arr_option = ['order'=>['key'=>'id','value'=>'desc']];
            $data = AppRepository::get_list($arr_option);
            $data = list_page_data($data);
            return  hello_success('成功',$data);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 添加一条数据
     *
     * @param $arr_input
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            //添加
            $res = AppRepository::add_one($arr_input)->toarray();
            if(empty($res)){
                return hello_error('添加失败');
            }

            return  hello_success('成功',$res);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id)
    {
        try {
            $n_id = (int) $n_id;
            if($n_id <= 0) {
                hello_error('数据ID不能为空');
            }
            //查询
            $res = AppDownloadRepository::base_find_one($n_id)->toarray();
            if(empty($res)){
                return hello_error('查询失败');
            }

            return  hello_success('查询成功',$res);
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 修改一条数据
     * @param array $arr_input
     * @return array
     */
    public function edit_one(array $arr_input)
    {
        $arr_input = hi_array_del_null($arr_input);
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = AppDownloadRepository::base_edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 修改单个字段
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state($arr_input){
        try {
            $arr_input['id'] = (int)$arr_input['id'];
            if ($arr_input['id'] <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = AppRepository::edit_one($arr_input);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');
        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one($n_id)
    {
        try {
            if ($n_id <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = AppRepository::delete_one($n_id);
            if($res === false){
                return hello_error('操作失败');
            }
            return  hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }
}